package com.huike.clues.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huike.clues.domain.TbCourse;
import com.huike.clues.domain.dto.TbCourseDTO;
import com.huike.clues.result.TbCoursel;
import com.huike.clues.service.TbCourseService;
import com.huike.clues.mapper.TbCourseMapper;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.StringUtils;
import com.huike.common.utils.bean.BeanUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

/**
 * @author 93238
 * @description 针对表【tb_course(课程管理)】的数据库操作Service实现
 * @createDate 2023-10-12 06:35:46
 */
@Service
@RequiredArgsConstructor
public class TbCourseServiceImpl extends ServiceImpl<TbCourseMapper, TbCourse>
        implements TbCourseService {

    private final TbCourseMapper tbCourseMapper;

    /**
     * 新增课程
     * @param tbCourseDTO
     */
    @Override
    public void course(TbCourseDTO tbCourseDTO) {

        TbCourse tbCourse = new TbCourse();
        BeanUtils.copyProperties(tbCourseDTO, tbCourse);
        tbCourse.setCreateTime(LocalDateTime.now().toString());
        tbCourse.setCode(StringUtils.getRandom(8));
        tbCourse.setIsDelete(0);
        tbCourseMapper.course(tbCourse);

    }

    /**
     * 查询课程管理接口
     *
     * @param tbCoursel
     * @return
     */
    @Override
    public TableDataInfo findById(TbCoursel tbCoursel) {
        PageHelper.startPage(tbCoursel.getPageNum(),tbCoursel.getPageSize());

        if (tbCoursel.getParams().get("beginCreateTime") != null && !(tbCoursel.getParams().get("beginCreateTime").equals(""))
                && tbCoursel.getParams().get("endCreateTime") != null && !(tbCoursel.getParams().get("endCreateTime").equals("")))
        {
            String beginCreateTime = (String) tbCoursel.getParams().get("beginCreateTime");
            String endCreateTime = (String) tbCoursel.getParams().get("endCreateTime");
            LocalDateTime beginCreateDateTime = LocalDateTime.of(LocalDate.parse(beginCreateTime), LocalTime.MIN);
            LocalDateTime endCreateDateTime = LocalDateTime.of(LocalDate.parse(endCreateTime), LocalTime.MAX);
            tbCoursel.setBeginCreateTime(beginCreateDateTime);
            tbCoursel.setEndCreateTime(endCreateDateTime);
        }

        Page<TbCourseDTO> page = tbCourseMapper.pageQuery(tbCoursel);

        TableDataInfo tableDataInfo = new TableDataInfo<>();
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setRows(page.getResult());
        return tableDataInfo;

    }

    /**
     * 修改课程管理接口
     * @param tbCourseDTO
     */
    @Override
    public void update(TbCourseDTO tbCourseDTO) {

        TbCourse tbCourse = new TbCourse();
        BeanUtils.copyProperties(tbCourseDTO,tbCourse);
        //tbCourseMapper.update(tbCourse);
    }


    /**
     * 删除课程管理
     * @param ids
     */
    @Override
    @Transactional
    public void delete(List<Long> ids) {

        for (Long id : ids) {
            tbCourseMapper.deleteByIds(id);
        }
    }

    /**
     * 获取课程管理详细信息
     * @param id
     * @return
     */
    @Override
    public Object getByCourseId(Long id) {

        //return (tbCourseMapper.getByCourseId(id));
        return null;
    }


    /**
     * 查询课程管理列表
     * @param tbCourse 课程管理
     * @return 课程管理
     */
    // @Override
    // public List<TbCourse> selectTbCourseList(TbCourse tbCourse)
    // {
    //     return tbCourseMapper.selectTbCourseList(tbCourse);
    // }
}




